package com.yanggeng.demo.beginnerguidedemo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yanggeng.demo.beginnerguidedemo.R;

/**
 * Created by yanggeng on 2017/11/8.
 */

public class ScalePagerIndicator extends LinearLayout {

    private int tabCount; // 指示器个数

    private int selectPosition; // 当前选中位置

    private float offset = 0f;

    private int dotColor; // 指示点颜色

    private int dotRadius; // 指示点半径

    private int textColor; // 文字颜色

    private int textSize; // 文字大小

    private int selectTextColor; // 选中文字颜色

    private int selectTextSize; // 选中文字大小

    private Context mContext;
    private ViewPager mViewPager;
    private PagerAdapter mPagerAdapter;
    private PagerStateChangeListener pagerStateChangeListener = new PagerStateChangeListener();


    public ScalePagerIndicator(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScalePagerIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        setOrientation(LinearLayout.HORIZONTAL);
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        DisplayMetrics dm = getResources().getDisplayMetrics();

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ScalePagerIndicator, defStyleAttr, 0);
        int n = typedArray.getIndexCount();

        for (int i = 0; i < n; i ++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
//                case R.styleable.ScalePagerIndicator_dotColor:
//                    dotColor = typedArray.getColor(attr, Color.WHITE);
//                    break;
//                case R.styleable.ScalePagerIndicator_dotRadius:
//                    dotRadius = typedArray.getDimensionPixelSize(attr, 2);
//                    break;
                case R.styleable.ScalePagerIndicator_indicatorTextColor:
                    textColor = typedArray.getColor(attr, Color.WHITE);
                    break;
                case R.styleable.ScalePagerIndicator_indicatorTextSize:
                    textSize = typedArray.getDimensionPixelSize(attr,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15,dm));
                    break;
                case R.styleable.ScalePagerIndicator_selectTextColor:
                    selectTextColor = typedArray.getColor(attr, Color.WHITE);
                    break;
                case R.styleable.ScalePagerIndicator_selectTextSize:
                    selectTextSize = typedArray.getDimensionPixelSize(attr,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20,dm));
                    break;
            }
        }
        typedArray.recycle();
    }

    private void setViewPager(ViewPager viewPager) {
        this.mViewPager = viewPager;
        if (viewPager.getAdapter() == null) {
            throw new IllegalStateException(
                    "ViewPager does not has adapter instance");
        } else {
            this.mPagerAdapter = viewPager.getAdapter();
        }
        viewPager.addOnPageChangeListener(pagerStateChangeListener);
    }

    class PagerStateChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//            selectPosition = position;
            offset = positionOffset;

            // 滚动

            invalidate();
        }

        @Override
        public void onPageSelected(int position) {
            selectPosition = position;

            // 更新指示器状态
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            // 滑动状态为停止时
            if (state == ViewPager.SCROLL_STATE_IDLE) {
//                scrollToChild(viewPager.getCurrentItem(), 0);
            }
        }
    }

}
